Passed
Push — main ( 97c2d8...520393 )
by Ehsan
09:14 queued 04:28
created

Tweet.isReply   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
import {Twitter} from 'twit';
2 4
import Helper from '../Helper';
3 4
import {ReTweetableAbstract, Validation} from '../ReTweetable';
4 4
import TweetUser, {TweetUserConfig} from './TweetUser';
5
6
export type TweetConfig = {
7
    minFavs: number,
8
    minFavsToFollowers: number,
9
    hashtagsLimit: number,
10
    userConfig: TweetUserConfig,
11
    wordBlocklist?: string[]
12
};
13
14 4
export default class Tweet extends ReTweetableAbstract {
15
    rawTweet: Twitter.Status;
16
    tweetUser: TweetUser;
17
    config: TweetConfig;
18
19
    constructor(rawTweet: Twitter.Status, config: TweetConfig) {
20 40
        super();
21 40
        this.rawTweet = rawTweet;
22 40
        this.tweetUser = new TweetUser(this.rawTweet.user, config.userConfig);
23 40
        this.config = config;
24
    }
25
26
    isRetweet(): boolean {
27 20
        return this.rawTweet.retweeted || Helper.objectExists(this.rawTweet.retweeted_status);
28
    }
29
30
    isReply(): boolean {
31 15
        return Helper.objectExists(this.rawTweet.in_reply_to_screen_name)
32
        || Helper.objectExists(this.rawTweet.in_reply_to_status_id)
33
        || Helper.objectExists(this.rawTweet.in_reply_to_status_id_str)
34
        || Helper.objectExists(this.rawTweet.in_reply_to_user_id)
35
        || Helper.objectExists(this.rawTweet.in_reply_to_user_id_str);
36
    }
37
38
    isSensitive(): boolean {
39 14
        return Helper.objectExists(this.rawTweet.possibly_sensitive) && this.rawTweet.possibly_sensitive;
40
    }
41
42
    hasMinFavs(): boolean {
43 12
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count >= this.config.minFavs;
44
    }
45
46
    hasMinFavsToFollowersRatio(): boolean {
47 11
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count / this.rawTweet.user.followers_count >= this.config.minFavsToFollowers;
48
    }
49
50
    hasTooManyHashtags(): boolean {
51 10
        return this.rawTweet.entities.hashtags.length > this.config.hashtagsLimit;
52
    }
53
54
    isWithheld(): boolean {
55 9
        return Helper.objectExists(this.rawTweet.withheld_copyright) && this.rawTweet.withheld_copyright;
56
    }
57
58
    hasBlockListedWord(): boolean {
59 10
        if (!Helper.objectExists(this.config.wordBlocklist)
60
        || !Helper.objectExists(this.rawTweet.text)) {
61 6
            return false;
62
        }
63
64 4
        const tweetText = this.rawTweet.text.toLowerCase();
65
66 6
        return this.config.wordBlocklist.some(blockListedWord => tweetText.includes(blockListedWord.toLowerCase()));
67
    }
68
69
    getRetweetValidations(): Validation[] {
70 15
        return [
71
            {
72 15
                validate: () => this.isRetweet(),
73 2
                message: () => 'Tweet is retweet'
74
            },
75
            {
76 13
                validate: () => this.isReply(),
77 1
                message: () => 'Tweet is reply'
78
            },
79
            {
80 12
                validate: () => this.isSensitive(),
81 2
                message: () => 'Tweet is sensitive'
82
            },
83
            {
84 10
                validate: () => !this.hasMinFavs(),
85 1
                message: () => 'Tweet does not have min favs'
86
            },
87
            {
88 9
                validate: () => !this.hasMinFavsToFollowersRatio(),
89 1
                message: () => 'Tweet does not have min favs to followers'
90
            },
91
            {
92 8
                validate: () => this.hasTooManyHashtags(),
93 1
                message: () => 'Tweet has too many hashtags'
94
            },
95
            {
96 7
                validate: () => this.isWithheld(),
97 1
                message: () => 'Tweet is withheld'
98
            },
99
            {
100 6
                validate: () => this.hasBlockListedWord(),
101 1
                message: () => 'Tweet has block listed word in it'
102
            },
103
            {
104 5
                validate: () => !this.tweetUser.isReTweetable(),
105 1
                message: () => this.tweetUser.retweetError
106
            },
107
        ];
108
    }
109
}